package com.shinhye.elephantsafe.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shinhye.elephantsafe.R;

public class SetStatusRelativeLayout extends RelativeLayout {

	private TextView tv_desc;
	private CheckBox cb;
	private String desc_on;
	private String name;
	private String desc_off;
	private TextView tv_name;

	public SetStatusRelativeLayout(Context context) {
		super(context);
		init(context);
	}

	public SetStatusRelativeLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public SetStatusRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		
		int name_resVal = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/com.shinhye.elephantsafe", "name", 0);
		name = getContext().getString(name_resVal);
		int on_resVal = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/com.shinhye.elephantsafe", "desc_on", 0);
		desc_on = getContext().getString(on_resVal);
		int off_resVal = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/com.shinhye.elephantsafe", "desc_off", 0);
		desc_off = getContext().getString(off_resVal);
		
		setSettingName(name);
		setDesc(desc_on);
	}
	
	private void init(Context context) {
		//将item_setting挂载为SetStatusRelativeLayout的子View，这样就可以findViewById查到了
		View.inflate(context, R.layout.item_setting_setstatus, SetStatusRelativeLayout.this);
		tv_name = (TextView) findViewById(R.id.tv_setting_name1);
		tv_desc = (TextView) findViewById(R.id.tv_setting_desc1);
		cb = (CheckBox) findViewById(R.id.cb_setting1);
	}
	
	public boolean isChecked() {
		return cb.isChecked();
	}
	
	public void setSettingName(String name) {
		tv_name.setText(name);
	}
	
	public void setChecked(boolean ch) {
		if(ch) {
			setDesc(desc_on);
		} else {
			setDesc(desc_off);
		}
		cb.setChecked(ch);
	}
	
	public void setDesc(String desc) {
		tv_desc.setText(desc);
	}
	
}
